﻿
Partial Class App_Follow
    Inherits MyUI.PageBaseApp

    Private FolderParentId As Integer
    Protected SortGridQuery, SortGridFollStaus As UI.SortControl
    Public WithEvents GroupWiz As UI.GroupButton(Of LinkButton)
    Protected CurrAccount As mDB.Account

#Region "PAGE"
    Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load

        CurrAccount = CurrUser.CurrAccount

        Me.JsVar("root_folder_id") = CurrUser.Tab_Folder(GloukDB.Folder.Type.TWITTER_QUERY, 0).ToObj.Id
        Me.JsVar("tree_type_id") = CInt(mDB.Folder.GetFolderType(mDB.Folder.DataType.QUERY, CurrAccount.Network_Id))
        Me.JsVar("webservice_url") = Me.ResolveClientUrl("~/Webservice/Folder.asmx/")
        Me.JsVar("icons_path") = Me.ResolveClientUrl("~/Style/App/img/Icons/")

        FolderParentId = Utils.Conv.ToInt(Me.HidSelectFolderId.Value)

        SortGridQuery = New UI.SortControl(Me.ListQuery.ID, Me.ViewState, "Query", SortDirection.Ascending)

        GroupWiz = New UI.GroupButton(Of LinkButton)(Me.ViewState, "WIZ_TAB", "tab-active")



        If Not Me.IsPostBack Then
            SetDataPage()
        Else
        End If

    End Sub

    Public Sub SetDataPage()

        SetDataWizard()
        SetDataOptions()

        SetListFollowQuery()
        SetListFollowProfile()
        SetListListenQuery()
        SetListEnquiry()

        'SetListFollowStatus()

    End Sub

#End Region


#Region "WIZARD"

    Protected Sub SetDataWizard()

        If Not Me.IsPostBack Then
            Me.GroupWiz.SelectButton = Me.BtnTabWiz1
        End If

        Me.PnlWiz1.Visible = False
        Me.PnlWiz2.Visible = False
        Me.PnlWiz3.Visible = False


        If Me.PnlWizFinish.Visible Then
            Me.TxtWiz1Query1.Text = "#"
            Me.TxtWiz1Query2.Text = "#"
            Me.TxtWiz1Query3.Text = "#"
            Me.TxtWiz1Query4.Text = "#"

            Me.TxtWiz2Profile1.Text = "@"
            Me.TxtWiz2Profile2.Text = "@"
            Me.TxtWiz2Profile3.Text = "@"
            Me.TxtWiz2Profile4.Text = "@"

            Me.TxtWiz3Query1.Text = "#"
            Me.TxtWiz3Query2.Text = "#"
            Me.TxtWiz3Query3.Text = "#"
            Me.TxtWiz3Query4.Text = "#"

        Else

            Select Case Me.GroupWiz.SelectValue
                Case 1
                    Me.PnlWiz1.Visible = True
                Case 2
                    Me.PnlWiz2.Visible = True
                Case 3
                    Me.PnlWiz3.Visible = True
            End Select

        End If



    End Sub

    Protected Sub GroupWiz_Selected(value As String) Handles GroupWiz.Selected
        Me.PnlWizFinish.Visible = False
        SetDataWizard()
    End Sub

    Protected Sub BtnWizAddFollQuery_Click(sender As Object, e As EventArgs) Handles BtnWizAddFollQuery.Click

        AddQueryAndAutoFoll(Me.TxtWiz1Query1.Text)
        AddQueryAndAutoFoll(Me.TxtWiz1Query2.Text)
        AddQueryAndAutoFoll(Me.TxtWiz1Query3.Text)
        AddQueryAndAutoFoll(Me.TxtWiz1Query4.Text)

        Me.PnlWizFinish.Visible = True

        Me.SetListEnquiry()
        Me.SetListFollowQuery()
        Me.SetDataWizard()

    End Sub

    Protected Sub BtnWizAddFollProfile_Click(sender As Object, e As EventArgs) Handles BtnWizAddFollProfile.Click

        AddProfileAndAutoFoll(Me.TxtWiz2Profile1.Text)
        AddProfileAndAutoFoll(Me.TxtWiz2Profile2.Text)
        AddProfileAndAutoFoll(Me.TxtWiz2Profile3.Text)
        AddProfileAndAutoFoll(Me.TxtWiz2Profile4.Text)

        Me.PnlWizFinish.Visible = True

        Me.SetListEnquiry()
        Me.SetListFollowProfile()
        Me.SetDataWizard()

    End Sub
    Protected Sub BtnWizAddListenQuery_Click(sender As Object, e As EventArgs) Handles BtnWizAddListenQuery.Click

        AddQueryAndListen(Me.TxtWiz3Query1.Text)
        AddQueryAndListen(Me.TxtWiz3Query2.Text)
        AddQueryAndListen(Me.TxtWiz3Query3.Text)
        AddQueryAndListen(Me.TxtWiz3Query4.Text)

        Me.PnlWizFinish.Visible = True

        Me.SetListEnquiry()
        Me.SetListListenQuery()
        Me.SetDataWizard()

    End Sub


    Private Sub AddQueryAndAutoFoll(q As String)

        If q <> "#" Then

            If Not MyApp.GetUserLimitPlan(GloukDB.Plan.LimitType.AUTO_FOLLOWER_PER_ACCOUNT, True, CurrAccount.Id) Then

                Dim Query = (New mDB.Query With {.Folder_Id = Me.FolderParentId, .Query = q}).SetDefault
                DB.TAB.Query.InsertOnSubmit(Query)
                DB.SubmitChanges()

                Dim QueryFoll = New mDB.Auto_Follow_Query With {.Account_Id = CurrAccount.Id, .Query_Id = Query.Id, .Accuracy_Total_Account = 140 * 4}
                DB.TAB.Auto_Follow_Query.InsertOnSubmit(QueryFoll)
                DB.SubmitChanges()

            End If

        End If

    End Sub
    Private Sub AddProfileAndAutoFoll(q As String)

        If q <> "@" Then

            If Not MyApp.GetUserLimitPlan(GloukDB.Plan.LimitType.AUTO_FOLLOWER_PER_ACCOUNT, True, CurrAccount.Id) Then

                Dim tw = New MyApp.Network.Twitter With {.oauth = CurrAccount.GetOauth}
                Dim AccToAdd As New List(Of String)
                AccToAdd.Add(q.Replace("@", ""))

                Dim Accs = tw.DownloadAccounts(AccToAdd, Nothing)

                If Accs.Count > 0 Then
                    Dim Profile = New mDB.Profile With {.Folder_Id = FolderParentId, .Account_Id = Accs(0).Id, .Post_Download_Count = -1}
                    DB.TAB.Profile.InsertOnSubmit(Profile)
                    DB.SubmitChanges()

                    Dim ProfileFoll = New mDB.Auto_Follow_Profile With {.Account_Id = CurrAccount.Id, .Profile_Account_Id = Profile.Account_Id}
                    DB.TAB.Auto_Follow_Profile.InsertOnSubmit(ProfileFoll)
                    DB.SubmitChanges()

                End If

            End If


        End If

    End Sub
    Private Sub AddQueryAndListen(q As String)

        If q <> "#" Then

            If Not MyApp.GetUserLimitPlan(GloukDB.Plan.LimitType.LISTEN_PER_ACCOUNT, True, CurrAccount.Id) Then

                Dim Query = (New mDB.Query With {.Folder_Id = Me.FolderParentId, .Query = q}).SetDefault
                DB.TAB.Query.InsertOnSubmit(Query)
                DB.SubmitChanges()

                Dim QueryListen = New mDB.Listen_Query With {.Account_Id = CurrAccount.Id, .Query_Id = Query.Id}
                DB.TAB.Listen_Query.InsertOnSubmit(QueryListen)
                DB.SubmitChanges()

            End If

        End If

    End Sub

#End Region

#Region "OPTION"

    Protected Sub SetDataOptions()

        Dim dd = New Dictionary(Of Integer, String) From {
            {0, "0"},
            {50, "50"},
            {100, "100"},
            {200, "200"},
            {300, "300"},
            {500, "500"}
        }
        UI.List.Bind(Me.DdlAutoFollNumber, dd, "key", "value", CurrAccount.Account_Option.Follow_Day_Limit, True)


    End Sub

    Protected Sub BtnOptionSave_Click(sender As Object, e As EventArgs) Handles BtnOptionSave.Click

        CurrAccount.Account_Option.Follow_Day_Limit = CInt(Me.DdlAutoFollNumber.SelectedValue)
        CurrAccount.Account_Option.Unfollow_Day_Limit = CurrAccount.Account_Option.Follow_Day_Limit * 1.5

        DB.SubmitChanges()


    End Sub


#End Region

#Region "AUTO FOLLOW QUERY"

    Public Sub SetListFollowQuery()
        Dim list = Me.ListAutoFollQuery

        Dim dt = CurrAccount.Tab_Auto_Follow_Query.
            REL.Auto_Follow_Query.Query.
            SetSelect("query.id", "query", "followed_update", "accuracy_total_account").
        ToDataTable()

        UI.ListData.Bind(list, dt, {"id"})

        Me.UpPAutoFollwQuery.Update()

    End Sub

    Protected Sub ListAutoFollQuery_ItemCommand(sender As Object, e As ListViewCommandEventArgs) Handles ListAutoFollQuery.ItemCommand

        Dim id = UI.ListDataItem.GetKey(e.CommandSource)

        If id Is Nothing Then
            Exit Sub
        End If

        Dim Row = UI.ListDataItem.GetRow(e.CommandSource)

        Dim Follquery = CurrAccount.Tab_Auto_Follow_Query(id).ToObj

        Select Case e.CommandName

            Case "REMOVE_QUERY"

                'CurrAccount.Tab_Auto_Follow_Query(query.Id).Delete.ExecuteCommand()
                DB.DeleteOnSubmit(Follquery)
                DB.SubmitChanges()

                Me.SetListEnquiry()
                Me.SetListFollowQuery()


        End Select
    End Sub

    'Protected Sub BtnUpdateFollQuery_Click(sender As Object, e As System.EventArgs) Handles BtnUpdateFollQuery.Click
    '    Dim g = Me.GridFollowQuery

    '    Dim RecEditID = (From k In g.DataKeys Select k.valUe).ToList
    '    If RecEditID.Count > 0 Then

    '        Dim Records = CurrAccount.Tab_Auto_Follow_Query.Where("Query_Id", "in", RecEditID).ToDictionary(Of Integer)("Query_Id")
    '        For ct As Integer = 0 To g.Rows.Count - 1

    '            Records(g.DataKeys(ct).Value).Accuracy_Total_Account = Utils.Conv.ToDouble(UI.Grid.GetTextBox(g, ct, "TxtAccuracyTotalAccount").Text)

    '        Next

    '        DB.SubmitChanges()

    '    End If

    '    Me.SetListFollowQuery()


    'End Sub


#End Region

#Region "AUTO FOLLOW PROFILE"

    Public Sub SetListFollowProfile()
        Dim g = Me.ListAutoFollProfile


        Dim dt = CurrAccount.Tab_Auto_Follow_Profile.
            REL.Auto_Follow_Profile.Account.
            REL.Account.Account_Add_Info.
        ToDataTable()

        'g.PageSize = 30
        'UI.Grid.Bind(g, dt, {"id"})

        UI.ListData.Bind(g, dt, {"id"})

        Me.UpPAutoFollProfile.Update()

    End Sub

    Protected Sub ListAutoFollProfile_ItemCommand(sender As Object, e As ListViewCommandEventArgs) Handles ListAutoFollProfile.ItemCommand

        Dim id = UI.ListDataItem.GetKey(e.CommandSource)

        If id Is Nothing Then
            Exit Sub
        End If

        Dim Row = UI.ListDataItem.GetRow(e.CommandSource)

        Dim FollProfile = CurrAccount.Tab_Auto_Follow_Profile(id).ToObj


        Select Case e.CommandName

            Case "REMOVE_PROFILE"

                'CurrAccount.Tab_Auto_Follow_Query(query.Id).Delete.ExecuteCommand()
                'CurrAccount.Tab_Auto_Follow_Profile(FollProfile.Profile_Account_Id).Delete.ExecuteCommand()
                DB.DeleteOnSubmit(FollProfile)
                DB.SubmitChanges()
                Me.SetListEnquiry()
                Me.SetListFollowProfile()


        End Select
    End Sub

#End Region

#Region "LISTEN QUERY"

    Public Sub SetListListenQuery()
        Dim g = Me.ListListenQuery


        Dim dt = CurrAccount.Tab_Query_Listen.ToDataTable

        'g.PageSize = 30
        'UI.Grid.Bind(g, dt, {"id"})

        UI.ListData.Bind(g, dt, {"id"})

        Me.UpPListenQuery.Update()

    End Sub

    Protected Sub ListListenQuery_ItemCommand(sender As Object, e As ListViewCommandEventArgs) Handles ListListenQuery.ItemCommand

        Dim id = UI.ListDataItem.GetKey(e.CommandSource)

        If id Is Nothing Then
            Exit Sub
        End If

        Dim Row = UI.ListDataItem.GetRow(e.CommandSource)

        Dim Listenquery = CurrAccount.Tab_Listen_Query(id).ToObj 'DB.TAB.Query.GetByPK(id)


        Select Case e.CommandName

            Case "REMOVE_QUERY"

                DB.DeleteOnSubmit(Listenquery)
                DB.SubmitChanges()
                SetListEnquiry()
                Me.SetListListenQuery()


        End Select
    End Sub

#End Region

#Region "ENQUIRY"

    Public Sub SetListEnquiry()

        SetListQuery()
        SetListProfile(False)

        Me.UpPEnquiry.Update()

    End Sub


    Public Sub SetListQuery()

        Me.SetListQuery(False)

    End Sub
    Public Sub SetListQuery(AddRow As Boolean)

        Me.SetListQuery(AddRow, New mDB.Query)

    End Sub
    Public Sub SetListQuery(AddRow As Boolean, QueryDef As mDB.Query)
        Dim list = Me.ListQuery

        Dim dt = SortGridQuery.SortQuery(CurrUser.View_Query(FolderParentId).SetSelect("query.*")).ToList

        If AddRow Then
            'Utils.DT.AddDataRowAt(qs, UI.Grid.GetIndexDataFromRow(g, 0))
            dt.Insert(UI.ListData.GetIndexData(list, 0), QueryDef)
            list.EditIndex = 0
        End If

        'query che segui l'utente

        '-------------------------------------------------------
        ' LAYOUT
        '-------------------------------------------------------

        Dim DataBind = UI.ListData.Bind(list, dt, {"id"})

        ' Dim DataBind As List(Of mDB.Query) = list.DataSource


        Dim QueryFoll = CurrAccount.Tab_Auto_Follow_Query.ToDictionary(Of Integer)("query_id")
        Dim QueryListen = CurrAccount.Tab_Listen_Query.ToDictionary(Of Integer)("query_id")

        For Each gr In list.Items
            Dim Data As mDB.Query = DataBind(UI.ListDataItem.GetIndexData(gr))

            MyUI.Data.SetRowStatusQuery(gr, Data)

            If gr.DisplayIndex <> list.EditIndex Then

                Dim BtnAddAutoFollQuery = UI.ListDataItem.GetControl(Of LinkButton)(gr, "BtnAddAutoFollQuery")
                Dim BtnAddListenQuery = UI.ListDataItem.GetControl(Of LinkButton)(gr, "BtnAddListenQuery")
                Dim BtnEditRow = UI.ListDataItem.GetControl(Of LinkButton)(gr, "BtnEditRow")

                UI.Controls.ClassSet(BtnAddAutoFollQuery, QueryFoll.ContainsKey(Data.Id), "a-icona-mano-h", "a-icona-mano")
                UI.Controls.ClassSet(BtnAddListenQuery, QueryListen.ContainsKey(Data.Id), "a-icona-canc-v", "a-icona-canc")

                If Data.Last_Update = Date.MinValue Then
                    BtnEditRow.Enabled = True
                Else
                    BtnEditRow.Enabled = False
                    UI.Controls.ClassReplace(BtnEditRow, "a-icona-matita", "en-vuoto")
                End If


            End If

        Next



    End Sub
    Protected Sub BtnAddQuery_Click(sender As Object, e As System.EventArgs) Handles BtnAddQuery.Click

        If Not MyApp.GetUserLimitPlan(GloukDB.Plan.LimitType.QUERY_PER_USER, True) Then
            SetListQuery(True)
        End If

    End Sub
    Protected Sub BtnAddProfile_Click(sender As Object, e As System.EventArgs) Handles BtnAddProfile.Click

        If Not MyApp.GetUserLimitPlan(GloukDB.Plan.LimitType.QUERY_PER_USER, True) Then
            SetListProfile(True)
        End If

    End Sub

    Public Sub SetListProfile()

        SetListProfile(False)

    End Sub


    Public Sub SetListProfile(AddRow As Boolean)
        Dim list = Me.ListProfile

        Dim q = CurrUser.Tab_Profile(FolderParentId).
            REL.Profile.Account.REL.Account.Account_Add_Info

        Dim dt = q.ToDataTable

        If AddRow Then
            Utils.DT.AddDataRowAt(dt, UI.ListData.GetIndexData(list, 0))
            'dt.Insert(UI.ListData.GetIndexData(Me.ListQuery, 0), QueryDef)
            list.EditIndex = 0
        End If

        Dim Profiles = q.ToList

        'query che segui l'utente
        Dim ProfileFoll = CurrAccount.Tab_Auto_Follow_Profile.ToDictionary(Of Integer)("profile_account_id")


        '-------------------------------------------------------
        ' LAYOUT
        '-------------------------------------------------------

        'g.PageSize = 30
        Dim DataBind = UI.ListData.Bind(list, dt, {"id"})

        'Dim pro = Utils.DT.ToList(Of mDB.Profile)(dt, "")

        For Each gr In list.Items
            'Dim Data = DataBind(UI.ListDataItem.GetIndexData(gr))
            Dim Data = Utils.DT.ToObj(Of mDB.Profile)(DataBind(UI.ListDataItem.GetIndexData(gr)))


            If gr.DisplayIndex <> list.EditIndex Then

                MyUI.Data.SetRowStatusProfile(gr, Data)
                Dim BtnAddAutoFollProfile = UI.ListDataItem.GetControl(Of LinkButton)(gr, "BtnAddAutoFollProfile")
                UI.Controls.ClassSet(BtnAddAutoFollProfile, ProfileFoll.ContainsKey(Data.Account_Id), "a-icona-mano-h", "a-icona-mano")

            End If


        Next

    End Sub



    Protected Sub BtnReload_UpPEnquiry_Click(sender As Object, e As System.EventArgs) Handles BtnReload_UpPEnquiry.Click

        SetListEnquiry()

    End Sub
    Protected Sub ListQuery_ItemCommand(sender As Object, e As ListViewCommandEventArgs) Handles ListQuery.ItemCommand
        Dim list As ListView = sender

        Dim id = UI.ListDataItem.GetKey(e.CommandSource)

        If id Is Nothing Then
            Exit Sub
        End If

        Dim Row = UI.ListDataItem.GetRow(e.CommandSource)

        Dim query = DB.TAB.Query.GetByPK(id)

        Select Case e.CommandName

            Case "CANCEL_ROW_EDIT"

                list.EditIndex = -1
                SetListQuery(False)

            Case "UPDATE_ROW"

                If query Is Nothing Then
                    query = (New mDB.Query With {.Folder_Id = Me.FolderParentId}).SetDefault
                    DB.TAB.Query.InsertOnSubmit(query)
                End If

                query.Query = UI.ListDataItem.GetTextBox(Row, "TxtQuery").Text
                query.Place = UI.ListDataItem.GetTextBox(Row, "TxtPlace").Text
                query.Lang_Code = UI.ListDataItem.GetTextBox(Row, "TxtLangCode").Text
                'query.Description = UI.ListDataItem.GetTextBox(gr, "TxtDescription").Text
                query.Network_Id = CurrAccount.Network_Id

                DB.SubmitChanges()

                list.EditIndex = -1
                SetListQuery(False)

            Case "EDIT_ROW"
                list.EditIndex = Row.DisplayIndex
                SetListQuery(False)

            Case "DELETE_ROW"
                DB.TAB.Query.DeleteOnSubmit(query)
                DB.SubmitChanges()
                SetListQuery(False)

            Case "ADD_AUTO_FOLL_QUERY"
                If Not MyApp.GetUserLimitPlan(GloukDB.Plan.LimitType.AUTO_FOLLOWER_PER_ACCOUNT, True, CurrAccount.Id) Then
                    Dim QueryFoll = CurrAccount.Tab_Auto_Follow_Query(query.Id).ToObj

                    If QueryFoll Is Nothing Then
                        QueryFoll = New mDB.Auto_Follow_Query With {.Account_Id = CurrAccount.Id, .Query_Id = query.Id, .Accuracy_Total_Account = 140 * 4}
                        DB.TAB.Auto_Follow_Query.InsertOnSubmit(QueryFoll)
                    Else
                        DB.DeleteOnSubmit(QueryFoll)
                    End If
                    DB.SubmitChanges()

                    Me.SetListQuery(False)
                    Me.SetListFollowQuery()
                End If

                'Case "UNFOLLOW"

                '    CurrAccount.Tab_FollowingQuery(query.Id).Delete.ExecuteCommand()
                '    Me.SetListQuery(False)
                '    Me.SetListAccountFollowQuery()

            Case "ADD_LISTEN_QUERY"
                'query.Real_Time = 1
                If Not MyApp.GetUserLimitPlan(GloukDB.Plan.LimitType.LISTEN_PER_ACCOUNT, True, CurrAccount.Id) Then

                    Dim QueryListen = CurrAccount.Tab_Listen_Query(query.Id).ToObj
                    If QueryListen Is Nothing Then
                        QueryListen = New mDB.Listen_Query With {.Account_Id = CurrAccount.Id, .Query_Id = query.Id}
                        DB.TAB.Listen_Query.InsertOnSubmit(QueryListen)
                    Else
                        DB.DeleteOnSubmit(QueryListen)
                    End If
                    DB.SubmitChanges()

                    Me.SetListQuery(False)
                    Me.SetListListenQuery()
                End If


        End Select
    End Sub
    Protected Sub ListProfile_ItemCommand(sender As Object, e As ListViewCommandEventArgs) Handles ListProfile.ItemCommand
        Dim List As ListView = sender
        Dim id = UI.ListDataItem.GetKey(e.CommandSource)
        If id Is Nothing Then
            Exit Sub
        End If
        Dim Row = UI.ListDataItem.GetRow(e.CommandSource)

        Dim Profile = DB.TAB.Profile.GetByPK(id)

        Select Case e.CommandName


            Case "CANCEL_ROW_EDIT"

                List.EditIndex = -1
                SetListQuery(False)

            Case "UPDATE_ROW"


                Dim tw = New MyApp.Network.Twitter
                tw.oauth = CurrAccount.GetOauth
                Dim Txtusername = UI.ListDataItem.GetTextBox(Row, "Txtusername")

                Dim AccToAdd As New List(Of String)
                AccToAdd.Add(Txtusername.Text.Replace("@", ""))


                Dim Accs = tw.DownloadAccounts(AccToAdd, Nothing)

                If Accs.Count > 0 Then
                    Dim Prof = New mDB.Profile With {
                        .Folder_Id = FolderParentId, .Account_Id = Accs(0).Id, .Post_Download_Count = -1
                    }

                    DB.TAB.Profile.InsertOnSubmit(Prof)
                    'CurrTask.Share_Account_Id = Accs(0).Id
                    'CurrTask.Share_Account_Username = Me.TxtShareAccountUsername.Text
                    DB.SubmitChanges()

                End If


                List.EditIndex = -1
                SetListProfile(False)

            Case "EDIT_ROW"
                List.EditIndex = Row.DisplayIndex
                SetListQuery(False)

            Case "DELETE_ROW"

                DB.TAB.Profile.DeleteOnSubmit(Profile)
                DB.SubmitChanges()
                SetListProfile(False)

            Case "ADD_AUTO_FOLL_PROFILE"
                If Not MyApp.GetUserLimitPlan(GloukDB.Plan.LimitType.AUTO_FOLLOWER_PER_ACCOUNT, True, CurrAccount.Id) Then
                    Dim ProfileFoll = CurrAccount.Tab_Auto_Follow_Profile(Profile.Account_Id).ToObj

                    If ProfileFoll Is Nothing Then
                        ProfileFoll = New mDB.Auto_Follow_Profile With {.Account_Id = CurrAccount.Id, .Profile_Account_Id = Profile.Account_Id}
                        DB.TAB.Auto_Follow_Profile.InsertOnSubmit(ProfileFoll)
                    Else
                        DB.DeleteOnSubmit(ProfileFoll)
                    End If
                    DB.SubmitChanges()

                    Me.SetListEnquiry()
                    Me.SetListFollowProfile()
                End If

        End Select
    End Sub



    'Protected Sub GridQuery_RowCommand(sender As Object, e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles GridQuery.RowCommand
    '    Dim g As GridView = sender

    '    Dim id = UI.Grid.GetKey(g, e.CommandSource)

    '    If id Is Nothing Then
    '        Exit Sub
    '    End If

    '    Dim gr = UI.GridRow.GetRow(e.CommandSource)

    '    Dim query = DB.TAB.Query.GetByPK(id)


    '    Select Case e.CommandName


    '        Case "VIEW"

    '            g.SelectedIndex = gr.RowIndex
    '            'Dim dt = query.Tab_QueryPost.ToDataTable

    '            Me.HidSelectQueryId.Value = query.Id

    '            Me.SetListAccountQuery()

    '        Case "FOLLOW"
    '            If Not MyApp.GetUserLimitPlan(GloukDB.Plan.LimitType.AUTO_FOLLOWER_PER_ACCOUNT, True, CurrAccount.Id) Then
    '                Dim QueryFoll = CurrAccount.Tab_Auto_Follow_Query(query.Id).ToObj

    '                If QueryFoll Is Nothing Then
    '                    QueryFoll = New mDB.Auto_Follow_Query With {.Account_Id = CurrAccount.Id, .Query_Id = query.Id, .Accuracy_Total_Account = 140 * 4}
    '                    DB.TAB.Auto_Follow_Query.InsertOnSubmit(QueryFoll)
    '                    DB.SubmitChanges()
    '                End If

    '                Me.SetListEnquiry()
    '                Me.SetListFollowQuery()
    '            End If

    '            'Case "UNFOLLOW"

    '            '    CurrAccount.Tab_FollowingQuery(query.Id).Delete.ExecuteCommand()
    '            '    Me.SetListQuery(False)
    '            '    Me.SetListAccountFollowQuery()


    '    End Select
    'End Sub
    'Protected Sub GridQuery_Sorting(sender As Object, e As System.Web.UI.WebControls.GridViewSortEventArgs) Handles GridQuery.Sorting

    '    Me.SortGridQuery.Sort(e.SortExpression)

    '    Me.SetListQuery(False)
    'End Sub
    'Protected Sub GridQuery_PageIndexChanging(sender As Object, e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles GridQuery.PageIndexChanging

    '    GridQuery.PageIndex = e.NewPageIndex

    '    Me.SetListQuery(False)
    'End Sub



#End Region

#Region "AUTO FOLLOW STATUS"

    Public Sub SetListFollowStatus()
        Dim g = Me.GridFollowStatus

        Dim dt = SortGridFollStaus.SortQuery(
            CurrAccount.Tab_Auto_Follow_Account.
            REL.Auto_Follow_Account.Query.
            Select("query.query")
        ).ToDataTable()

        g.PageSize = 30
        UI.Grid.Bind(g, dt)

        For Each gr As GridViewRow In g.Rows
            Dim dr = dt(UI.GridRow.GetIndexData(gr))
            Dim LblStatus = UI.GridRow.GetControl(Of Label)(gr, "LblStatus")

            Dim Status = DirectCast(dr("status_id"), mDB.Auto_Follow_Account.StatusType)

            Select Case Status
                Case mDB.Auto_Follow_Account.StatusType.PENDING, mDB.Auto_Follow_Account.StatusType.FOLLOW
                    LblStatus.ForeColor = Drawing.Color.Orange

                Case mDB.Auto_Follow_Account.StatusType.ALREADY_FRIEND, mDB.Auto_Follow_Account.StatusType.UNFOLLOW
                    LblStatus.ForeColor = Drawing.Color.Red

                Case mDB.Auto_Follow_Account.StatusType.ERROR
                    LblStatus.ForeColor = Drawing.Color.Gray

                Case Else
                    LblStatus.ForeColor = Drawing.Color.Green

            End Select


            LblStatus.Text = Status.ToString

        Next


        Me.UpPFollowStatus.Update()

    End Sub

    Protected Sub GridFollowStatus_Sorting(sender As Object, e As System.Web.UI.WebControls.GridViewSortEventArgs) Handles GridFollowStatus.Sorting

        Me.SortGridFollStaus.Sort(e.SortExpression)

        Me.SetListFollowStatus()

    End Sub

    Protected Sub GridFollowStatus_PageIndexChanging(sender As Object, e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles GridFollowStatus.PageIndexChanging

        GridFollowStatus.PageIndex = e.NewPageIndex

        Me.SetListFollowStatus()
    End Sub

#End Region

#Region "POST"

    Protected Sub GridAccounts_PageIndexChanging(sender As Object, e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles GridAccounts.PageIndexChanging
        Dim g As GridView = sender

        g.PageIndex = e.NewPageIndex

        SetListAccountQuery()

    End Sub

    Protected Sub GridQueryPosts_PageIndexChanging(sender As Object, e As System.Web.UI.WebControls.GridViewPageEventArgs)
        Dim g As GridView = sender

        Dim id = UI.Grid.GetKey(Me.GridAccounts, sender)

        g.PageIndex = e.NewPageIndex

        SetListPostAccountQuery(g, id)


    End Sub

    Private Sub SetListAccountQuery()
        Dim query = DB.TAB.Query.GetByPK(Me.HidSelectQueryId.Value)


        Dim dt = query._Tab_Query_Post.
            GroupBy("account_code").
            OrderBy("post_count desc").
            SetSelect("account_code", "account_username", "count(account_code) as post_count").
        ToDataTable
        '
        Me.GridAccounts.PageSize = 20
        UI.Grid.Bind(Me.GridAccounts, dt, {"account_code"})
        Me.HidSelectQueryId.Value = query.Id

        Me.UpPQueryResult.Update()


    End Sub

    Private Sub SetListPostAccountQuery(grid As GridView, AccountCode As String)
        Dim query = DB.TAB.Query.GetByPK(Me.HidSelectQueryId.Value)

        Dim dt As DataTable
        dt = query._Tab_Query_Post.Where("account_code", "=", AccountCode).ToDataTable

        grid.PageSize = 10
        UI.Grid.Bind(grid, dt)

    End Sub

    Protected Sub GridAccounts_RowCommand(sender As Object, e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles GridAccounts.RowCommand

        Dim g As GridView = sender

        Dim id = UI.Grid.GetKey(g, e.CommandSource)

        If id Is Nothing Then
            Exit Sub
        End If

        Dim gr = UI.GridRow.GetRow(e.CommandSource)

        Select Case e.CommandName

            Case "VIEW"

                Dim grid = UI.GridRow.GetControl(Of GridView)(gr, "GridQueryPosts")

                SetListPostAccountQuery(grid, id)

                grid.Visible = Not grid.Visible


        End Select



    End Sub

#End Region



End Class
